package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.CancelOrderForm;
import com.cskaoyan.order.form.OrderDetail;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @author: 叶熠彬
 * @description:
 * @date: 2022/7/29 23:06
 */
@RestController
@RequestMapping("shopping")
public class OrderController {

    @Autowired
    OrderQueryService queryService;

    @Autowired
    OrderCoreService coreService;

    /**
     * 创建订单
     *
     * @author: 叶熠彬
     * @date: 2022/7/29 23:15
     * @param: [request]
     * @return: com.cskaoyan.mall.commons.result.ResponseData
     */
    @PostMapping("order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(jsonObject.get("uid").toString());
        // 设置uid
        request.setUserId(uid);
        // 设置unique_key
        request.setUniqueKey(UUID.randomUUID().toString());
        CreateOrderResponse response = coreService.createOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(Integer.parseInt(response.getCode()), response.getMsg());
    }

    /**
     * 查询全部订单
     *
     * @author: 叶熠彬
     * @date: 2022/7/30 11:45
     * @param: [request, servletRequest]
     * @return: com.cskaoyan.mall.commons.result.ResponseData
     */

    @GetMapping("order")
    public ResponseData getOrderList(OrderListRequest request, HttpServletRequest servletRequest) {
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(jsonObject.get("uid").toString());
        request.setUserId(uid);
        OrderListResponse response = queryService.orderList(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(Integer.parseInt(response.getCode()), response.getMsg());
    }

    /**
     * 获取订单详细信息
     *
     * @author: 叶熠彬
     * @date: 2022/7/30 12:24
     * @param: [id]
     * @return: com.cskaoyan.mall.commons.result.ResponseData
     */
    @GetMapping("order/{id}")
    public ResponseData getOrderDetail(@PathVariable("id") String id) {
        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(id);
        OrderDetail response = queryService.orderDetail(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(Integer.parseInt(response.getCode()), response.getMsg());
    }

    /**
     * 取消订单
     *
     * @author: 叶熠彬
     * @date: 2022/7/31 15:49
     * @param: [id]
     * @return: com.cskaoyan.mall.commons.result.ResponseData
     */
        @PostMapping("cancelOrder")
        public ResponseData cancelOrder(@RequestBody CancelOrderRequest request) {
        CancelOrderResponse response = coreService.cancelOrder(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(Integer.parseInt(response.getCode()), response.getMsg());
    }

    /**
     * 删除订单
     *
     * @author: 叶熠彬
     * @date: 2022/7/31 16:59
     * @param: [id]
     * @return: com.cskaoyan.mall.commons.result.ResponseData
     */
    @RequestMapping(path = "order/{id}", method = RequestMethod.DELETE)
    public ResponseData deleteOrder(@PathVariable("id") String id) {
        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(id);
        DeleteOrderResponse response = coreService.deleteOrder(request);


        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(Integer.parseInt(response.getCode()), response.getMsg());
    }

}
